package mmc.resource.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import mmc.action.Action;
import mmc.action.BartenderKey;
import mmc.ui.KeyBinding;

public class SavedVariables {
	public static void saveConfig(){
		Map<Integer, String> map = new HashMap<Integer, String>();
		
		for(Entry<Integer, Action> a : KeyBinding.binding.entrySet())
		{
			//System.out.println(a.toString());
			map.put(a.getKey(), a.toString()); //.getValue().getClass().getName());
		}
		
		try {
		      FileOutputStream fout = new FileOutputStream("saveBindings.dat");
		      ObjectOutputStream oos = new ObjectOutputStream(fout);
		      oos.writeObject(map);
		      oos.close();
	    }
		catch (Exception e) { e.printStackTrace(); }
	}
	
	@SuppressWarnings("unchecked")
	public static Map<Integer,String> loadConfig(){
		Map<Integer,String> keys = null;
		try {
			    FileInputStream fin = new FileInputStream("saveBindings.dat");
			    ObjectInputStream ois = new ObjectInputStream(fin);
			    keys = (Map<Integer,String>) ois.readObject();
			    ois.close();
		}
		catch (FileNotFoundException nfe){ }
		catch (Exception e) { e.printStackTrace(); }
		
		return keys;
	}
	
	
	
	public static void saveSettings(List<?> list, String file){
		Map<Integer, String> map = new HashMap<Integer, String>();
		
		for(Object o : list)
		{
			BartenderKey act = (BartenderKey)o;
			
			if(act.action != null)
				map.put(act.defaultKey, act.action.getClass().getName());
		}
		
		try {
		      FileOutputStream fout = new FileOutputStream(file);
		      ObjectOutputStream oos = new ObjectOutputStream(fout);
		      oos.writeObject(map);
		      oos.close();
	    }
		catch (Exception e) { e.printStackTrace(); }
	}
	
	@SuppressWarnings("unchecked")
	public static Map<Integer,String> loadSettings(String file){
		Map<Integer,String> keys = null;
		try {
			    FileInputStream fin = new FileInputStream(file);
			    ObjectInputStream ois = new ObjectInputStream(fin);
			    keys = (Map<Integer,String>) ois.readObject();
			    ois.close();
		}
		catch (FileNotFoundException nfe){ }
		catch (Exception e) { e.printStackTrace(); }
		
		return keys;
	}
}
